#include "main.h"
#include "DXSetup.h"
#include "Global.h"
#include "CWorld.h"
#include "Collision.h"

// The main entry point of our Windows program
CWorld* World; 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	// Create and fill out a window class structure

	WNDCLASS wcex;
	ZeroMemory(&wcex,sizeof(WNDCLASS));
	
	// Provide the address of the function Windows should call with messages	
	wcex.lpfnWndProc= (WNDPROC)WndProc;
	wcex.style= CS_HREDRAW | CS_VREDRAW;
	wcex.hInstance= hInstance;
	wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszClassName= "MyWindowClass";

	// Register my new window class with the Windows API
	RegisterClass(&wcex);
    DWORD style=WS_POPUP|WS_VISIBLE;

	// Create the window
	HWND hWnd = CreateWindow("MyWindowClass", "Learning DX", 
		style, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
		NULL, NULL, hInstance, NULL);
	
	
	if (hWnd==0)
		return -1;

	// Set up Direct3D
	if (!DXSetup(hWnd,hInstance))
		return -1;

	//Create
	g_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);

	//Timer Stuff
	int testt1=0;
	float testt2=0;

	// Show the window for the first time
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	D3DInputDevice->Acquire();
	BYTE keys[256];
	DIMOUSESTATE mouse_state;	

	// Get the input's device state, and put the state in keys
	ZeroMemory(keys, sizeof(keys) );
	D3DInputDevice->GetDeviceState( sizeof(keys), keys );

	for(int a=0;a<360;a++){
		Cos[a]=cos((float)a);
		Sin[a]=sin((float)a);
	}
	g_pWorld= new CWorld();
	g_pCollisionMan = new CCollisionManager();

	MSG msg;
	ZeroMemory( &msg, sizeof(msg) );
	while( msg.message!=WM_QUIT )
	{
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{	
			HRESULT	hr=D3DInputDevice->GetDeviceState( sizeof(keys), keys );
	
			if (FAILED(hr)){
				// If input is lost then acquire and keep trying until we get it back
				hr=D3DInputDevice->Acquire();
				while( hr == DIERR_INPUTLOST ){        
					hr = D3DInputDevice->Acquire();
				}
				// Now get them again	
				D3DInputDevice->GetDeviceState( sizeof(keys), keys );
			}
			if (FAILED(D3DMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state)))
				return false;
			//Step
			g_pWorld->Step(keys, mouse_state);

			g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
								NULL, 1.0f, 0 );

			if (SUCCEEDED(g_pD3DDevice->BeginScene()))	{	
				
				//Draw
				g_pWorld->Draw();
				// End the scene
				g_pD3DDevice->EndScene();
			}
			g_pD3DDevice->Present(NULL,NULL,NULL,NULL);

			if (keys[DIK_F10] & 0x80 || g_pWorld->Exit())
				msg.message=WM_QUIT;

		}
	}
	
	Cleanup();
	g_pWorld->CleanUp();
	return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		case WM_DESTROY:
            PostQuitMessage(0);
		default:
			// We do not want to handle this message so pass it back to Windows
			// to handle it in a default way
			if(!(g_pWorld!=NULL && g_pWorld->WndProc(hWnd, uMsg, wParam, lParam)))
				return DefWindowProc(hWnd, uMsg, wParam, lParam);
		break;
	}

	return 0;
}
